package com.hl.toolkit.server.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static boolean isNull(String s) {
		if (null == s || "".equals(s) || "".equals(s.trim()) || "null".equalsIgnoreCase(s)) {
			return true;
		} else {
			return false;
		}
		// return str == null || "".equals(str.trim());
	}
	
	public static boolean isNotNull(String s) {
		if (null == s || "".equals(s) || "".equals(s.trim()) || "null".equalsIgnoreCase(s)) {
			return false;
		} else {
			return true;
		}
		// return str == null || "".equals(str.trim());
	}


	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}

	/**
	 * 乘法计算
	 */
	public static double mul(String str1, String str2) {
		/*
		 * 使用BigDecimal不会有误差 要求必须使用String类型构造器
		 */
		if (isNull(str1)) {
			str1 = "0";
		}
		if (isNull(str2)) {
			str2 = "0";
		}
		BigDecimal b1 = new BigDecimal(str1);
		BigDecimal b2 = new BigDecimal(str2);
		BigDecimal b3 = b1.multiply(b2);
		return b3.doubleValue();
	}

	/**
	 * 进行加法运算
	 */
	public static double add(String d1, String d2) {
		if (isNull(d1)) {
			d1 = "0";
		}
		if (isNull(d2)) {
			d2 = "0";
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.add(b2).doubleValue();
	}

	public static double doubleFormat(double d) {
		// DecimalFormat df=new DecimalFormat(".##");
		java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.00");
		return Double.valueOf(myformat.format(d));
	}

	/**
	 * 手机号验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isMobile(String str) {
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
		m = p.matcher(str);
		b = m.matches();
		return b;
	}

	public static String splitArray(String str[]) {
		String temp = "";
		if (str != null && str.length > 0) {
			for (String s : str) {
				temp += s;
				temp += ",";
			}
			return temp.substring(0, temp.length() - 1);
		} else {
			return temp;
		}
	}

	public static boolean timeOut(String startTime, int milliSecond) throws Exception {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(startTime);
		long time1 = now.getTime();
		long time2 = date.getTime();
		long cha = Math.abs(time1 - time2);
		if (cha > milliSecond) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取订单号
	 * @param remark
	 * @return
	 */
	public static String getNo(String remark) {
		String str = "";
		if (!isNull(remark)) {
			str = remark;
		}
		str = str + new SimpleDateFormat("yyMMddHHmm").format(new Date());
		Random r = new Random();
		str = str + "" + r.nextInt(1000);
		return str;
	}
	
	/**
	 * 汉语转拼音
	 * @param ChineseLanguage
	 * @return
	 */

	/*public String toHanyuPinyin(String ChineseLanguage) {
		char[] cl_chars = ChineseLanguage.trim().toCharArray();
		String hanyupinyin = "";
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
		defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		try {
			for (int i = 0; i < cl_chars.length; i++) {
				if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
					hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
				} else {// 如果字符不是中文,则不转换
					hanyupinyin += cl_chars[i];
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			System.out.println("字符不能转成汉语拼音");
		}
		return hanyupinyin;
	}*/
	
	/**
	 * 验证非空参数  返回状态码
	 * @param map
	 * @return
	 */
	public static Map<String,Object>  isNotparamReturnMap(Map<String,String> map){
		Map<String, Object> mapCode=new HashMap<>();
 		for (Map.Entry<String, String> entry : map.entrySet()) {  
  		     if(!StringUtils.isNotBlank(entry.getValue())){
  		    	mapCode.put("code", 201);
  		    	mapCode.put("msg", entry.getKey()+"参数不能为空");	             
	        }
		}  
		return mapCode;
		
	}
	/**
	 * 解码
	 */
	public static String toGBK(String unicodeStr,String code) { 
		try { 
		String gbkStr = new String(unicodeStr.getBytes("ISO8859-1"), code); 
		return gbkStr; 
		} catch (UnsupportedEncodingException e) { 
		return unicodeStr; 
		} 
		} 

}
